//
//  ZSMainViewController.swift
//  ZSWeibo
//
//  Created by apple on 15/11/23.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit

class ZSMainViewController: UITabBarController {

    ///撰写按钮点击事件
    func composeClick()
    {
        print("hahahahaha")
    }
    
    
    
    

    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
//        //设置自定义的tabBar
//        let newTabBar = ZSTabBar()
//        //KVC
//        setValue(newTabBar, forKey: "tabBar")
//        
//        //给撰写按钮添加点击事件
//        newTabBar.composeButton .addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        
        
        tabBar.tintColor = UIColor.orangeColor()
        
        //首页
        let homeVc = ZSHomeTableViewController()
        addChildViewController(homeVc, title: "首页", imageName: "tabbar_home")
        
        //消息
        let MessageVc = ZSMessageTableViewController()
    addChildViewController(MessageVc, title: "消息", imageName: "tabbar_message_center")
        
        //占位控制器
        addChildViewController(UIViewController())
        
        //发现
        let DiscoverVc = ZSDiscoverTableViewController()
        addChildViewController(DiscoverVc, title: "发现", imageName: "tabbar_discover")
        
        //我
        let ProfileVc = ZSProfileTableViewController()
        addChildViewController(ProfileVc, title: "我", imageName: "tabbar_profile")
        //在这里设置撰写按钮的frame太早了，会被占位item挡住
        //设置撰写按钮的frame
//        let width = tabBar.bounds.width / 5
//        let frame = CGRectMake(2 * width, 0, width, tabBar.bounds.height)
//        composeButton.frame=frame
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        //设置撰写按钮的frame
        let width = tabBar.bounds.width / 5
        let frame = CGRectMake(2 * width - 5, 0, width + 10, tabBar.bounds.height)
        composeButton.frame=frame
    }
    
    
    
    
    /*
        自定义添加tabbar子控制器
        controller：子控制器
        title：子控制器的标题   imageName：子控制器的图片名
        private： 表示只有本类或当前文件的其他类可以访问
    */
    
    private func addChildViewController(controller: UIViewController,title: String,imageName: String)
    {
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        //拼接高亮图片名称
//        let highLightImageName = imageName + "_highlighted"
        //让选中的图片使用原来的颜色，不要系统帮我们渲染
//        let highLightImage = UIImage(named: highLightImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        //设置高亮图片
//        controller.tabBarItem.selectedImage = highLightImage
        //设置标题颜色
//        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Highlighted)
        
        addChildViewController(UINavigationController(rootViewController: controller))
    }

    //懒加载 撰写按钮
    lazy var composeButton: UIButton = {
        //创建按钮
        let button = UIButton()
        
        //设置参数
        //设置按钮背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //设置按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        //将创建好的撰写按钮添加到tabbar里面
        self.tabBar.addSubview(button)
        //在这里添加按钮点击事件
        button.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        
        return button
        }()

}
